import React, { useEffect, useState } from 'react'
import './index.scss'
import Card from '../../dynamic/card'
import axios from '../../../utils/axios'
import NavBar from '../../../components/nav-bar'
import { message } from '../../../components/message'
import { loading } from '../../../components/loading'
import { useHistory } from 'react-router-dom'

export default function MyDynamic() {
  const [data, setData] = useState()
  const history = useHistory()
  useEffect(() => {
    getMyDynamic()
  }, [])
  const getMyDynamic = () => {
    let load = loading()
    axios
      .get(`/dynamic/getDynamicList?isme=${true}`)
      .then((res) => {
        console.log(res.data.data)
        setData(res.data.data)
        load.close()
      })
      .catch((err) => {
        message('加载失败啦~')
        load.close()
      })
  }
  const toDetail = (id) => {
    history.push(`/dynamic-detail?id=${id}`)
  }
  const toDelete = (id) => {
    axios
      .get(`/dynamic/deleteDynamic?dynamicId=${id}`)
      .then((res) => {
        console.log(res)
        message('删除成功~')
        getMyDynamic()
      })
      .catch((err) => {
        message('删除失败了orz~')
      })
  }
  return (
    <div className="my-dynamic">
      <NavBar roomName={'我的帖子'} showRight={false}></NavBar>
      <Card
        data={data}
        todetail={toDetail}
        query={getMyDynamic}
        showDelete
        toDelete={toDelete}
      ></Card>
    </div>
  )
}
